<?php

namespace App\Http\Controllers\admin;

use App\Http\Requests\UserInsertRequest;
use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
    public function getIndex(Request $request)
    {
      // dd($request->all());
        //判断是否有条件查询
        if ($request->input('keywords') && $request->input('bid')  ) {
          $p = DB::table('post')->select(DB::raw('*,count(followup.fid) as num'))->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->leftJoin('followup','post.pid','=','followup.fpid')->where('users.username','like','%'.$request->input('keywords').'%')->orWhere('post.pname','like','%'.$request->input('keywords').'%')->where('block.bid','=',$request->input('bid'))->groupBy('followup.fpid')->paginate(5);

          // $p = DB::select("select * from post left join block on post.bbid=block.bid join users on post.puid = users.uid where (username like '%".$request->input('keywords')."%' or pname like '%".$request->input('keywords')."%') and  bid = ".$request->input('bid'));
          // $p = DB::table('post')->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->where('users.username','like','%'.$request->input('keywords').'%')->orWhere('post.pname','like','%'.$request->input('keywords').'%')->where('block.bid','=',$request->input('bid'))->paginate(5);
        }else if($request->input('keywords')){
          $p = DB::table('post')->select(DB::raw('*,count(followup.fid) as num'))->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->leftJoin('followup','post.pid','=','followup.fpid')->where('users.username','like','%'.$request->input('keywords').'%')->orWhere('post.pname','like','%'.$request->input('keywords').'%')->groupBy('followup.fpid')->paginate(5);


          // $p = DB::table('post')->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->where('users.username','like','%'.$request->input('keywords').'%')->orWhere('post.pname','like','%'.$request->input('keywords').'%')->paginate(5);
          // $p = DB::select("select * from post left join block on post.bbid=block.bid join users on post.puid = users.uid where username like '%".$request->input('keywords')."%' or pname like '%".$request->input('keywords')."%'");
        }else if ($request->input('bid')) {
          $p = DB::table('post')->select(DB::raw('*,count(followup.fid) as num'))->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->leftJoin('followup','post.pid','=','followup.fpid')->where('block.bid','=',$request->input('bid'))->groupBy('followup.fpid')->paginate(5);

          // $p = DB::table('post')->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->where('block.bid','=',$request->input('bid'))->paginate(5);
          // $p = DB::select("select * from post left join block on post.bbid=block.bid join users on post.puid = users.uid where bid = ".$request->input('bid'));
        }else{
          $p = DB::table('post')->select(DB::raw('*,count(followup.fid) as num'))->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->leftJoin('followup','post.pid','=','followup.fpid')->groupBy('followup.fpid')->paginate(5);

          // $p = DB::table('post')->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->paginate(5);
          // $p = DB::select("select * from post left join block on post.bbid=block.bid join users on post.puid = users.uid");
        }
      //获取帖子数据
       // $p = DB::select("select * from post left join block on post.bid=block.bid join users on post.puid = users.uid");
      //获取板块列表
      $blocks = DB::table('block')->get();
      //获取list
      $list = $request->all();
      //解析模版  分配变量
    	return view('/admin.post.index',['p'=>$p,'blocks'=>$blocks,'list'=>$list]);
    }

    //删除
    public function postDelete(Request $request)
    {
        //获取pid
        $pid = $request->input('pid');
        $res = DB::table('post')->where('pid',$pid)->delete();
         if ($res) {
           //删除成功
          echo 0;
         }else{
          //删除失败
          echo 1;
         }
       
    }


    //js查询所有板块
    public function getSelect(Request $request)
    {
        $sid = $request->input('sid');
        //执行数据库操作
        $res = DB::select('select * from block where sid='.$sid);
        // dd($res);
        echo json_encode($res);
    }

    //js查询帖子状态
    public function getPstaus(Request $request)
    {
        $pid = $request->input('pid');
        $res = DB::select('select pstaus from post where pid= ? ',[$pid]);
        // dd($res);
        echo json_encode($res);
    }

    //修改帖子状态
    public function getUpdate(Request $request)
    {
        $data = $request->only('pid','pstaus');
        // dd($data['pid']);
        $res = DB::update("update post set pstaus = ".$data['pstaus']." where pid =  ? ",[$data['pid']]);
        
        echo $res;
    }


    //查询回帖信息
    public function getFollowup(Request $request)
    {
        $followup = DB::select("select *  from followup left join users on followup.fuid=users.uid where fpid = ? ",[$request->input('fpid')]);
        //已json格式回传数据
        echo json_encode($followup);
    }

}
